package com.mwcz.hot.work.controller;

import com.alibaba.fastjson.JSONObject;
import com.mwcz.hot.work.dao.CarDao;
import com.mwcz.hot.work.dao.SiteDao;
import com.utils.RestTemplateUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆管理 前端控制器
 *
 * @author Mr.Bai
 * @since 2021-06-18
 */
@Slf4j
@RestController
@RequestMapping("api/sensor")
@Api(tags = "传感器 -- 传感器接收")
public class SensorController {
    @Autowired
    private CarDao carDao;
    @Autowired
    private SiteDao siteDao;
    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @RequestMapping(value = "/position", method = RequestMethod.POST,produces = "application/json")
    public Map<String,Object> position(@RequestBody JSONObject param){
        Map<String,Object> result = new HashMap<>();
        String url ="https://restapi.amap.com/v3/assistant/coordinate/convert?locations="+param.get("longitude")+","+param.get("latitude")+"&coordsys=gps&output=json&key=2ba8b66872f4acb4f2c11144d36d8fc8";
        JSONObject resultJson = restTemplateUtil.getData(url);
        param.put("longitude",resultJson.getString("locations").split(",")[0]);
        param.put("latitude",resultJson.getString("locations").split(",")[1]);
        Integer flag =carDao.updateCarByCondition(param);
        Integer flag1 =siteDao.updateSiteByCondition(param);
        result.put("code",200);
        result.put("data",flag);
        result.put("message","接收成功");
        return result;
    }

    @RequestMapping(value = "/temperature", method = RequestMethod.POST,produces = "application/json")
    public Map<String,Object> temperature(@RequestBody JSONObject param){
        Map<String,Object> result = new HashMap<>();
        param.put("temperature",param.get("t1")+","+param.get("t2")+","+param.get("t3")+","+param.get("t4")+","+param.get("t5")+","+param.get("t6"));
        Integer flag =carDao.updateCarByCondition(param);
        Integer flag1 =siteDao.updateSiteByCondition(param);
        result.put("code",200);
        result.put("data",flag);
        result.put("message","接收成功");
        return result;
    }

    @RequestMapping(value = "/heat", method = RequestMethod.POST,produces = "application/json")
    public Map<String,Object> heat(@RequestBody JSONObject param){
        Map<String,Object> result = new HashMap<>();
        Integer flag =carDao.updateCarByCondition(param);
        Integer flag1 =siteDao.updateSiteByCondition(param);
        result.put("code",200);
        result.put("data",flag);
        result.put("message","接收成功");
        return result;
    }
}
